// 2025/11/8
// 三数之和

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        vector<vector<int>> ans;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < n - 2; i++)
        {
            if(i > 0 && nums[i] == nums[i - 1])
                continue;
            int l = i + 1, r = n - 1;
            while(l < r)
            {
                int sum = nums[i] + nums[l] + nums[r];
                if(sum < 0) l++;
                else if(sum > 0) r--;
                else
                {
                    ans.push_back({nums[i], nums[l], nums[r]});
                    for(l = l + 1; l < r && nums[l] == nums[l - 1]; l++);
                    for(r = r - 1; l < r && nums[r] == nums[r + 1]; r--);
                }
            }
        }
        return ans;
    }
};